package com.minyifang.wcspf.work.ui.user.presenter;

import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.work.ui.user.view.AddAddressView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/6/25.
 */
public class AddAddressPresenter extends BasePresenter<AddAddressView> {
    public void getStoreList() {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), Constants.getJsonData(null, UserManager.getInstance().getToken()).toString());
        addTask(RetrofitHelper.getInstance().getService().getAllStoreList(requestBody), respond -> getView().showStoreList(respond));
    }

    public void getXiaoQuList(String storeId) {
        Map<String, Object> map = new HashMap<>();
        map.put("storeId", storeId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getXiaoQuList(requestBody), respond -> getView().showXiaoQuList(respond));
    }

    public void addAddress(String edtName, String edtPhone, String selectStoreId, String selectXiaoQuId, String edtAddressInfo, String idDef) {
        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("name", edtName);
        map.put("mobile", edtPhone);
        map.put("storeId", selectStoreId);
        map.put("storeAreaId", selectXiaoQuId);
        map.put("address", edtAddressInfo);
        map.put("isDefault", idDef);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().saveAddress(requestBody), respond -> {
            getView().hideLoadingView();
            getView().showAddAddressRes(respond);
        });
    }

    public void editAddress(String id, String edtName, String edtPhone, String selectStoreId, String selectXiaoQuId, String edtAddressInfo, String idDef) {
        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("addressId", id);
        map.put("name", edtName);
        map.put("mobile", edtPhone);
        map.put("storeId", selectStoreId);
        map.put("storeAreaId", selectXiaoQuId);
        map.put("address", edtAddressInfo);
        map.put("isDefault", idDef);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().editAddress(requestBody), respond -> {
            getView().hideLoadingView();
            getView().showEditAddressRes(respond);
        });

    }
}
